package com.gw.booking.controller.merchant;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.UserAddDto;
import com.gw.booking.dto.wechat.WLoginDtoOut;
import com.gw.booking.dto.wechat.WMerchantDto;
import com.gw.booking.service.CustomerService;
import com.gw.booking.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

@Api(tags = {"商家-会员管理"})
@Controller
@RequestMapping("/merchant/customer")
public class MCustomerController {

    @Autowired
    private MerchantService merchantService;

    @ApiOperation(value = "微信登录（商家）")
    @RequestMapping(value = "/loginMerchant.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<WLoginDtoOut>> loginMerchant(HttpServletRequest request
            , @ApiParam(value = "微信code") @RequestParam(required = false) String code)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, merchantService.txLoginMerchant(code, request.getRemoteAddr()));
    }

    @ApiOperation(value = "微信绑定（商家）")
    @RequestMapping(value = "/addMerchant.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<PageDto<WLoginDtoOut>> addMerchant(HttpServletRequest request
            , @Valid @RequestBody WMerchantDto wMerchantDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        merchantService.txAddMerchant(wMerchantDto, request.getRemoteAddr());
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }
}
